package co.sanduche.teach;

import android.app.Fragment;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import co.sanduche.models.Enums;
import co.sanduche.services.EPClient;
import co.sanduche.services.EndPoints.UserEP;
import co.sanduche.teach.gallery.GalleryActivity;
import co.sanduche.util.DialogUtil;
import co.sanduche.util.EPCallback;
import co.sanduche.vos.LoginCredentials;

import com.android.volley.ServerError;

public class LoginFragment extends Fragment implements Button.OnClickListener {

	private static final String DEV_PASSWORD = "123";
	private static final String DEV_USER = "NUIPALGO1";
	private EditText identityDocumentField;
	private EditText passwordField;
	private LoginCredentials lc = new LoginCredentials() {
		{

			this.rememberMe = true;
			this.role = Enums.Role.TEACHER.name();
		}
	};
	private ProgressBar progressBar;

	public LoginFragment() {
	}

	public LoginCredentials getCredentials() {
		lc.setPassword(passwordField.getText().toString());
		lc.setUsername(identityDocumentField.getText().toString());
		return lc;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_login, container,
				false);

		identityDocumentField = ((EditText) rootView
				.findViewById(R.id.identity_document));
		passwordField = ((EditText) rootView.findViewById(R.id.password));

		if (EPClient.getDevPhase() == EPClient.DevPhase.Development) {
			identityDocumentField.setText(DEV_USER);
			passwordField.setText(DEV_PASSWORD);
		}

		progressBar = (ProgressBar) rootView
				.findViewById(R.id.progressBarLogin);
		Button button = ((Button) rootView.findViewById(R.id.login_button));
		button.setOnClickListener(this);

		return rootView;
	}

	@Override
	public void onClick(View v) {

		try {
			progressBar.setVisibility(View.VISIBLE);

			// Uncomment to bypass login
			// boolean a = true;
			// if (a) {
			// throw new RuntimeException("kaslasllaslaslaslaslaslaslasl");
			// Intent intent = new Intent(getActivity(),
			// GalleryActivity.class);
			// startActivity(intent);
			// getActivity().finish();
			// return;
			// } else
			// Uncomment to bypass login
			UserEP.login(getCredentials(), new EPCallback<Void>() {

				@Override
				public void onSuccess(Void result) {

					Intent intent = new Intent(getActivity(),
							GalleryActivity.class);
					startActivity(intent);
					getActivity().finish();
				}

				@Override
				public void onFailure(Throwable error) {
					super.onFailure(error);
					int status = -1;
					if (error instanceof ServerError) {
						ServerError e = (ServerError) error;
						status = e.networkResponse.statusCode;
					}
					progressBar.setVisibility(View.INVISIBLE);
					Resources r = getResources();
					String credenciales = r
							.getString(R.string.invalid_credencials);
					String servidor = r.getString(R.string.server_not_working);
					String message = status == 401 ? credenciales : servidor;

					DialogUtil.alert(LoginFragment.this, message);

				}
			});

		} catch (Exception e) {
			progressBar.setVisibility(View.INVISIBLE);
			// throw new RuntimeException(e);
		}

	}
}
